package com.larissa.android.quiz;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuProvider;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.NavDirections;
import androidx.navigation.NavGraph;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.larissa.android.quiz.databinding.ActivityMainBinding;
import com.larissa.android.quiz.databinding.FragmentHistoryBinding;
import com.larissa.android.quiz.databinding.HistoryItemBinding;

import java.util.Objects;

public class MainActivity extends AppCompatActivity
{
    private TextView Expression;
    private final String TAG="MainActivity";
    private String TestString;
    private View[] btnnum = new View[10];
    private View btnadd;
    private View btnsub;
    private View btnmult;
    private View btneq;

    private ActivityMainBinding activityMainBinding;
    MainViewModel mainViewModel;

    private MenuProvider menuProvider = new MenuProvider()
    {
        @Override
        public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater)
        {
            menuInflater.inflate(R.menu.main_menu, menu);
        }

        @Override
        public boolean onMenuItemSelected(@NonNull MenuItem menuItem)
        {
            changeFragment(menuItem.getItemId());
            return true;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate() called");
        setContentView(R.layout.activity_main);

        //        设置界面标题
        Objects.requireNonNull(getSupportActionBar()).setTitle("Calculator");

//        addMenuProvider(menuProvider);
        addMenuProvider(menuProvider);
        mainViewModel = new ViewModelProvider(this).get(MainViewModel.class);
        if(savedInstanceState == null)
        {
            showFragment();
        }
//        activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
//        View view =activityMainBinding.getRoot();
//        setContentView(view);

//        TestString = "";
//
//        Expression = findViewById(R.id.tv_Express);
//
//        int orientation=getResources().getConfiguration().orientation;
//        if(orientation== Configuration.ORIENTATION_PORTRAIT){               // 垂直布局
//            // 利用多态性补充代码。
//
//            btnnum[0] = findViewById(R.id.btn_num0);
//            btnnum[1] = findViewById(R.id.btn_num1);
//            btnnum[2] = findViewById(R.id.btn_num2);
//            btnnum[3] = findViewById(R.id.btn_num3);
//            btnnum[4] = findViewById(R.id.btn_num4);
//            btnnum[5] = findViewById(R.id.btn_num5);
//            btnnum[6] = findViewById(R.id.btn_num6);
//            btnnum[7] = findViewById(R.id.btn_num7);
//            btnnum[8] = findViewById(R.id.btn_num8);
//            btnnum[9] = findViewById(R.id.btn_num9);
//            btnadd    = findViewById(R.id.btn_add);
//            btnsub    = findViewById(R.id.btn_sub);
//            btnmult   = findViewById(R.id.btn_mult);
//        }
//        else if(orientation==Configuration.ORIENTATION_LANDSCAPE){          // 水平布局
//            // 利用多态性补充代码。
//
//        }
//        btnnum[0] = findViewById(R.id.btn_num0);
//        btnnum[1] = findViewById(R.id.btn_num1);
//        btnnum[2] = findViewById(R.id.btn_num2);
//        btnnum[3] = findViewById(R.id.btn_num3);
//        btnnum[4] = findViewById(R.id.btn_num4);
//        btnnum[5] = findViewById(R.id.btn_num5);
//        btnnum[6] = findViewById(R.id.btn_num6);
//        btnnum[7] = findViewById(R.id.btn_num7);
//        btnnum[8] = findViewById(R.id.btn_num8);
//        btnnum[9] = findViewById(R.id.btn_num9);
//        btnadd  = findViewById(R.id.btn_add);
//        btnsub    = findViewById(R.id.btn_sub);
//        btnmult   = findViewById(R.id.btn_mult);
//        btneq   = findViewById(R.id.btn_eq);
////        // 数字按钮点击监听器
//        int i;
//        for(i=0;i<10;i++)
//        {
//            btnnum[i].setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    btnClick(view);
//                }
//            });
//        }
//
//
//        btnadd.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                btnClick(view);
//            }
//        });
//        btneq.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                btnClick(view);
//            }
//        });
//        btnmult.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                btnClick(view);
//            }
//        });
//        btnsub.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                btnClick(view);
//            }
//        });
    }

    // 第一个数字不能为0
    private void btnClick(View v)
    {
        CompStack Testcomp = new CompStack("TestString");
//       代码实在是不够简洁，应该考虑是否有简化代码的方法
        if(v.getId() == R.id.btn_num0)
        {
            TestString = TestString + "0";
        }
        else if(v.getId() == R.id.btn_num1)
        {
            TestString = TestString + "1";
        }
        else if(v.getId() == R.id.btn_num2)
        {
            TestString = TestString + "2";
        }
        else if(v.getId() == R.id.btn_num3)
        {
            TestString = TestString + "3";
        }
        else if(v.getId() == R.id.btn_num4)
        {
            TestString = TestString + "4";
        }
        else if(v.getId() == R.id.btn_num5)
        {
            TestString = TestString + "5";
        }
        else if(v.getId() == R.id.btn_num6)
        {
            TestString = TestString + "6";
        }
        else if(v.getId() == R.id.btn_num7)
        {
            TestString = TestString + "7";
        }
        else if(v.getId() == R.id.btn_num8)
        {
            TestString = TestString + "8";
        }
        else if(v.getId() == R.id.btn_num9)
        {
            TestString = TestString + "9";
        }
        else if(v.getId() == R.id.btn_add)
        {
            TestString = TestString + "+";
        }
        else if(v.getId() == R.id.btn_sub)
        {
            TestString = TestString + "-";
        }
        else if(v.getId() == R.id.btn_mult)
        {
            TestString = TestString + "*";
        }
        else if(v.getId() == R.id.btn_eq)
        {
            TestString = String.valueOf(Testcomp.getAnswer(TestString));
        }

        Expression.setText(TestString);

    }

    private void changeFragment(int category)
    {
        if(category == R.id.menu_history)
        {
            mainViewModel.statusFlag = 1;           // 显示历史记录
            NavDirections action;
            action = MainFragmentDirections.toHistory();
            FragmentHistoryBinding fragmentHistoryBinding = FragmentHistoryBinding.inflate(getLayoutInflater());
//            View view=MenuInflater;
//            View view=getLayoutInflater().inflate(R.layout.history_item, fragmentHistoryBinding.getRoot(), false);
            Navigation.findNavController(this, R.id.fragment_container).navigate(action);
        }
//        showFragment();
    }
    private void showFragment()
    {
        NavHostFragment hostFragment=(NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_container);
        NavController controller=hostFragment.getNavController();
        NavGraph graph=controller.getNavInflater().inflate(R.navigation.nav_graph);
//        if(mainViewModel.statusFlag==0)
//        {
            graph.setStartDestination(R.id.mainFragment);
//        }
//        else if(mainViewModel.statusFlag==1)
//        {
//            graph.setStartDestination(R.id.historyFragment);
//        }
        controller.setGraph(graph);
    }
}